#include <iostream>
#include <sw/redis++/redis.h>
#include <vector>
#include <sw/redis++/redis++.h>
#include <string>
#include <unordered_map>
#include "util.hpp"
#include <set>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::unordered_map;
using std::vector;
using sw::redis::Redis;

void test1(Redis &redis)
{
    cout << "sadd and smembers" << endl;
    redis.flushall();

    long long ret = redis.sadd("key", {"111", "222", "22333", "666"});
    cout << ret << endl;
    std::set<string> result;
    auto it = std::inserter(result, result.end());
    redis.smembers("key", it);

    printContiner(result);
}

void test2(Redis &redis)
{
    cout << "sismember and spop and scard" << endl;
    redis.flushall();

    long long ret = redis.sadd("key", {"111", "222", "22333", "666"});

    cout << "result: " << redis.sismember("key", "22333") << endl;

    cout << "scard: " << redis.scard("key") << endl;

    auto result1 = redis.spop("key");
    if (result1)
        cout << "spop: " << result1.value() << endl;
    else
        cout << "元素无效" << endl;

    auto result2 = redis.spop("key");
    if (result2)
        cout << "spop: " << result2.value() << endl;
    else
        cout << "元素无效" << endl;
}

void test3(Redis &redis)
{
    cout << "sinter and sinterstore" << endl;
    redis.flushall();
    long long ret1 = redis.sadd("key1", {"111", "222", "22333", "666"});
    long long ret2 = redis.sadd("key2", {"111", "444", "22333", "2233"});
    cout << ret1 << " " << ret2 << endl;

    std::set<string> result;
    auto it = std::inserter(result, result.end());
    redis.sinter({"key1", "key2"}, it);
    cout << "sinter: " << endl;

    printContiner(result);
    cout << endl;

    long long retLen = redis.sinterstore("key3", {"key1", "key2"});
    cout << retLen <<endl;
    std::set<string> result1;
    auto it1 = std::inserter(result1, result1.end());
    redis.smembers("key3", it1);
    cout << "sinterstore: " << endl;
    
    printContiner(result1);
}

int main()
{
    Redis redis("tcp://127.0.0.1:6379");

    // test1(redis);
    // test2(redis);
    test3(redis);
    return 0;
}